package com.scut.fy.auth.model.dto;

import com.scut.fy.common.core.constant.FyConstant;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * TODO
 *
 * @author JY
 * @version 1.0
 * @date 2021/8/29
 */
public class UserDetailsDto implements UserDetails {
    private String username;
    private String password;
    private String lockStatus;
    private String delFlag;
    private List<SysRoleDto> roleList = new ArrayList<>();

    public UserDetailsDto(final SysUserDto sysUserDto, final List<SysRoleDto> roleList) {
        this.username = sysUserDto.getUserName();
        this.password = sysUserDto.getPassword();
        this.lockStatus = sysUserDto.getActiveFlag();
        this.delFlag = sysUserDto.getDelFlag();
        this.roleList = roleList;
    }

    public UserDetailsDto(String username, String password, String lockStatus, String delFlag) {
        this.username = username;
        this.password = password;
        this.lockStatus = lockStatus;
        this.delFlag = delFlag;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setLockStatus(String lockStatus) {
        this.lockStatus = lockStatus;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    public void setRoleList(List<SysRoleDto> roleList) {
        this.roleList = roleList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        final List<GrantedAuthority> authorityList = new ArrayList<>();
        for (SysRoleDto role : roleList) {
            authorityList.add(new SimpleGrantedAuthority(role.getRoleCode()));
        }
        return authorityList;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return FyConstant.STATUS_NORMAL.equals(this.lockStatus) ? true : false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return FyConstant.STATUS_NORMAL.equals(this.delFlag) ? true : false;
    }
}
